package com.thwj.project.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.thwj.common.annotation.Log;
import com.thwj.common.core.controller.BaseController;
import com.thwj.common.core.domain.AjaxResult;
import com.thwj.common.core.page.TableDataInfo;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.exception.ServiceException;
import com.thwj.common.utils.StringUtils;
import com.thwj.common.utils.poi.ExcelUtil;
import com.thwj.project.biz.controller.param.UserSearchParam;
import com.thwj.project.biz.controller.vo.SelectDataVo;
import com.thwj.project.biz.domain.User;
import com.thwj.project.biz.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 微信用户Controller
 *
 * @author generate
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/biz/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

/**
 * 查询微信用户列表
 */
@PreAuthorize("@ss.hasPermi('biz:user:list')")
@GetMapping("/list")
    public TableDataInfo list(User user) {
        startPage();
        List<User> list = userService.getBaseMapper().selectList(new QueryWrapper<>(user));
    return getDataTable(list,((Page)list).getTotal());
}


    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('biz:User:list')")
    @GetMapping("/searchListUser")
    public AjaxResult searchListUser(UserSearchParam userSearchParam) {
        List<User> list = userService.list(
                new QueryWrapper<User>()
                        .like(!StringUtils.isEmpty(userSearchParam.getSearchUserKey()),"user_nick_name", userSearchParam.getSearchUserKey())
                        .or()
                        .like(!StringUtils.isEmpty(userSearchParam.getSearchUserKey()),"phone", userSearchParam.getSearchUserKey())
                        .or()
                        .like(!StringUtils.isEmpty(userSearchParam.getSearchUserKey()),"contact_phone", userSearchParam.getSearchUserKey())
                        .last(" limit 20")
        );
        if(CollectionUtils.isEmpty(list)) {
            throw new ServiceException("关键字没有找到匹配的用户");
        }
        List<SelectDataVo> selectDataVoList = new ArrayList<>();
        list.forEach(item->{
            selectDataVoList.add(new SelectDataVo( item.getPhone() + "&" + item.getUserNickName(),item.getId()));
        });
        return AjaxResult.success(selectDataVoList);
    }

    /**
     * 导出微信用户列表
     */
    @PreAuthorize("@ss.hasPermi('biz:user:export')")
    @Log(title = "微信用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) {
        List<User> list = userService.list(new QueryWrapper<>(user));
        ExcelUtil<User> util = new ExcelUtil<User>(User. class);
        util.exportExcel(response, list, "微信用户数据");
    }

    /**
     * 获取微信用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(userService.getById(id));
    }

    /**
     * 新增微信用户
     */
    @PreAuthorize("@ss.hasPermi('biz:user:add')")
    @Log(title = "微信用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.save(user));
    }

    /**
     * 修改微信用户
     */
    @PreAuthorize("@ss.hasPermi('biz:user:edit')")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateById(user));
    }

    /**
     * 删除微信用户
     */
    @PreAuthorize("@ss.hasPermi('biz:user:remove')")
    @Log(title = "微信用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.removeByIds(Arrays.asList(ids)));
    }
}
